//
//  AppDelegate.m
//  CrossBorderPayment
//
//  Created by hc-101 on 2020/6/8.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "AppDelegate.h"
#import "TJMainController.h"
#import "TJBaseNavController.h"
#import "TJLoginController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

       [self setingLanguage];
       
       self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
       [self switchRootController];
       self.window.backgroundColor = UIColor.whiteColor;
       [self.window makeKeyAndVisible];
       
       [IQKeyboardManager sharedManager].enable = YES;
       [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
       [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    
    [self registeredNotification:YES];
    return YES;
}


- (void)switchRootController {
    if ([USERDEFAULTS boolForKey:isLogin]) {
        TJMainController *mainController = [[TJMainController alloc] init];
        mainController.selectedIndex = 0;
        self.window.rootViewController = mainController;
        [self loadUserData];
       
    } else {
        [self logOut];
    }
}

- (void)loadUserData {

    [TJNetworking.shareNetworking  updateUserInfoAndLoading:NO success:^(TJUserInfo * _Nonnull userInfo) {
        
    }];
    
}
- (void)logOut {
    [USERDEFAULTS setBool:NO forKey:isLogin];
    [TJUserInfo deleteInfo];
    self.window.rootViewController =  [[TJBaseNavController alloc] initWithRootViewController:[[TJLoginController alloc] init]];
}

- (void)registeredNotification:(BOOL)registered {
    
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    
    if (registered) {
        [notificationCenter addObserver:self selector:@selector(logOut) name:tokenErrorNotification object:nil];
        [notificationCenter addObserver:self selector:@selector(switchRootController) name:loginSucceedNotification object:nil];
//        [notificationCenter addObserver:self selector:@selector(logOut) name:cancellationNotification object:nil];
        [notificationCenter addObserver:self selector:@selector(switchRootController) name:languageChangedNotification object:nil];
    } else {
        [notificationCenter removeObserver:self];
    }
}

- (void)dealloc {
    [self registeredNotification: NO];
}

 
- (void)setingLanguage {
    if (![USERDEFAULTS objectForKey:appLanguage]) {
        NSArray *languages = [NSLocale preferredLanguages];
        NSString *language = [languages objectAtIndex:0];
        if ([language hasPrefix:Simplified]) {
            [USERDEFAULTS setObject:Simplified forKey:appLanguage];
        }else if([language hasPrefix:English]){
            [USERDEFAULTS setObject:English forKey:appLanguage];
        } else if([language containsString:Korean]){
            [USERDEFAULTS setObject:Korean forKey:appLanguage];
        }
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}


@end
